package com.lmx.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.RoleService;
import com.lmx.repoDomain.tmodel.Privilege;
import com.lmx.repoDomain.tmodel.Role;
import com.lmx.repoDomain.tmodel.RolePageInfo;
import com.lmx.repoDomain.tmodel.RolePrivilege;//huibati
import com.lmx.repoDomain.tmodel.UserPrivate;

@Controller
@RequestMapping("/backStand")
public class RoleController {

    @Autowired
    RoleService roleService;

    @Autowired
    HttpServletRequest request;
    
    //获取角色列表
    @RequestMapping(value = "/getRoleList", method = RequestMethod.GET)
    public String  getRoleList(ModelMap model,RolePageInfo rolePageInfo) {
    	
    	Map<String ,Object> values = new HashMap<String ,Object>();
    	
    	//验证当前页数
    	if(!String.valueOf(rolePageInfo.getPage()).isEmpty()){
    		int pageNum = (rolePageInfo.getPage()-1)*rolePageInfo.getSize();
    		values.put("pageNum", pageNum);
    		values.put("size", rolePageInfo.getSize());
    		model.put("page", rolePageInfo.getPage());
    	}else{
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	String roleName = rolePageInfo.getRoleName();
    	values.put("roleName", roleName);//角色名称
    	model.put("roleName", roleName==null?roleName="":roleName);
    	
    	//获取角色列表
    	List<Map<String ,Object>> roleList = roleService.getRoleList(values);
  
    	//获取角色列表总条数
    	int count = roleService.getRoleListCount(values);
    	
    	model.put("roleList", roleList);
    	model.put("count", count);
    	
        return "page/role/roleList";
    }    
    
    //添加角色
    @RequestMapping(value = "/addRole", method = RequestMethod.GET)
    @ResponseBody
    public String  addRole(String roleCode ,String roleName ,String remark) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	Role role1 = new Role();
    	role1.setCode(roleCode);
    	Role result1 = roleService.getRole(role1);
    	String resultroleCode = result1.getCode();
    	if(resultroleCode != null && !resultroleCode.isEmpty()){
    		msgMap.put("flag", "0");//code已存在
    		
    		json = CommonUtil.mapToJsonString(msgMap);
            return json;
    	}
    	
    	Role role2 = new Role();
    	role2.setName(roleName);
    	Role result2 = roleService.getRole(role2);
    	String resultroleName = result2.getName();
    	if(resultroleName != null && !resultroleName.isEmpty()){
    		msgMap.put("flag", "2");//name已存在
    		
    		json = CommonUtil.mapToJsonString(msgMap);
            return json;
    	}
    	Role role = new Role();
    	role.setCode(roleCode);
    	role.setName(roleName);
    	role.setRemark(remark);
    	
    	roleService.addRole(role);
    	
    	msgMap.put("flag", "1");//添加成功
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //获取角色详情
    @RequestMapping(value = "/getRoleInfo", method = RequestMethod.GET)
    @ResponseBody
    public String  getRoleInfo(String roleID) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	Role role = new Role();
    	role.setId(Long.parseLong(roleID));
    	role = roleService.getRole(role);

    	msgMap.put("flag", "1");
    	msgMap.put("role", role);
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //修改角色
    @RequestMapping(value = "/updateRoleInfo", method = RequestMethod.GET)
    @ResponseBody
    public String  updateRoleInfo(String roleCode ,String roleName ,String remark ,String roleID) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	Role role1 = new Role();
    	role1.setCode(roleCode);
    	Role result1 = roleService.getRole(role1);
    	String resultroleCode = result1.getCode();
    	if(resultroleCode != null && !resultroleCode.isEmpty()){
    		msgMap.put("flag", "0");//code已存在
    		
    		json = CommonUtil.mapToJsonString(msgMap);
            return json;
    	}
    	
    	Role role2 = new Role();
    	role2.setName(roleName);
    	Role result2 = roleService.getRole(role2);
    	String resultroleName = result2.getName();
    	if(resultroleName != null && !resultroleName.isEmpty()){
    		msgMap.put("flag", "2");//name已存在
    		
    		json = CommonUtil.mapToJsonString(msgMap);
            return json;
    	}
    	Role role = new Role();
    	role.setId(Long.parseLong(roleID));
    	role.setCode(roleCode);
    	role.setName(roleName);
    	role.setRemark(remark);
    	
    	roleService.updateRoleInfo(role);
    	
    	msgMap.put("flag", "1");//修改成功
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //删除角色
    @RequestMapping(value = "/deleteRole", method = RequestMethod.GET)
    @ResponseBody
    public String  deleteRole(String roleID) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	roleService.deleteRole(Long.parseLong(roleID));

    	msgMap.put("flag", "1");
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //跳转角色权限列表页面
    @RequestMapping(value = "/toRolePrivilegeListPage", method = RequestMethod.GET)
    public String  toRolePrivilegeListPage(ModelMap model, RolePageInfo rolePageInfo) {
    	
    	String roleID = rolePageInfo.getRoleID();
    	if(roleID == null){
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	
    	String roleName = rolePageInfo.getRoleName();
    	if(roleName == null){
    		model.put("erroMessage", "参数错误");
    		return "page/erro";
    	}
    	
    	String url = rolePageInfo.getUrl();
    	model.put("url", url==null?url="":url);
    	model.put("roleID", roleID==null?roleID="":roleID);
    	model.put("roleName", roleName==null?roleName="":roleName);
    	
    	return "page/role/rolePrivilegeList";
    }    
    
    //获取角色权限列表
    @ResponseBody
    @RequestMapping(value = "/getRolePrivilegeList", method = RequestMethod.GET)
    public String  getRolePrivilegeList(String roleID) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	List rolePrivilegeList = roleService.getRolePrivilegeList(Long.parseLong(roleID));
    	msgMap.put("rolePrivilegeList", rolePrivilegeList);

    	msgMap.put("flag", "1");
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //编辑角色权限
    @ResponseBody
    @RequestMapping(value = "/updateRolePrivilege", method = RequestMethod.GET)
    public String  updateRolePrivilege(String roleID,String privilegeIDStr) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	//删除该角色所有权限
    	roleService.deleteRolePricilege(Long.parseLong(roleID));
    	Role role = new Role();
    	role.setId(Long.parseLong(roleID));
    	
    	//添加该角色权限
    	if(privilegeIDStr != null && !privilegeIDStr.isEmpty()){
    		String [] privilegeIDArr= privilegeIDStr.split(",");
    		for(int i=0;i<privilegeIDArr.length;i++){
    			String privilegeID = privilegeIDArr[i];
    			Privilege privilege = new Privilege();
    			privilege.setId(Long.parseLong(privilegeID));
    			
    			RolePrivilege rolePrivilege = new RolePrivilege();
    			rolePrivilege.setRole(role);
    			rolePrivilege.setPrivilege(privilege);
    			roleService.addRolePricilege(rolePrivilege);
    		}
    	}

    	msgMap.put("flag", "1");//删除成功
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //获取权限列表
    @RequestMapping(value = "/getPrivilegeList", method = RequestMethod.GET)
    @ResponseBody
    public String  getPrivilegeList() {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	List privilegeList = roleService.getPrivilegeList();
    	msgMap.put("privilegeList", privilegeList);
    	msgMap.put("flag", "1");
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //跳转权限管理页面
    @RequestMapping(value = "/toPagePrivilegeList", method = RequestMethod.GET)
    public String  toPagePrivilegeList(ModelMap model) {
    	
        return "page/role/privilegeList";
    }
    
    //添加权限
    @RequestMapping(value = "/addPrivilege", method = RequestMethod.GET)
    @ResponseBody
    public String  addPrivilege(String showName,String code,String type,String level,String privilegeOrder,String moName,String pId,String remark ,String iconfile,String moIcon,String link) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	Privilege privilege = new Privilege();
    	if(showName != null && !showName.isEmpty()){
    		privilege.setShowName(showName);
    	}
    	if(code != null && !code.isEmpty()){
    		privilege.setCode(code);
    	}
    	if(type != null && !type.isEmpty()){
    		privilege.setType(Integer.parseInt(type));
    	}
    	if(level != null && !level.isEmpty()){
    		privilege.setLevel(Integer.parseInt(level));
    	}
    	if(privilegeOrder != null && !privilegeOrder.isEmpty()){
    		privilege.setOrder(Integer.parseInt(privilegeOrder));
    	}
    	if(moName != null && !moName.isEmpty()){
    		privilege.setMobileMenuName(moName);
    	}
    	if(pId != null && !pId.isEmpty()){
    		Privilege parent = new Privilege();
    		parent.setId(Long.parseLong(pId));
    		privilege.setParent(parent);
    	}
    	if(remark != null && !remark.isEmpty()){
    		privilege.setRemark(remark);
    	}
    	if(iconfile != null && !iconfile.isEmpty()){
    		privilege.setIcon(iconfile);
    	}
    	if(moIcon != null && !moIcon.isEmpty()){
    		privilege.setMobileIcon(moIcon);
    	}
    	if(link != null && !link.isEmpty()){
    		privilege.setLink(link);
    	}
    	
    	roleService.addPrivilege(privilege);
    	msgMap.put("flag", "1");
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //删除权限
    @RequestMapping(value = "/deletePrivilege", method = RequestMethod.GET)
    @ResponseBody
    public String  deletePrivilege(String privilegeID) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	roleService.deletePricilege(Long.parseLong(privilegeID));
    	msgMap.put("flag", "1");
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //根据权限等级获取权限列表
    @RequestMapping(value = "/getPrivilegeListByLevel", method = RequestMethod.GET)
    @ResponseBody
    public String  getPrivilegeListByLevel(String level) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	List<Privilege> privilegeList = roleService.getPrivilegeListByLevel(Integer.parseInt(level));
    	msgMap.put("privilegeList", privilegeList);
    	msgMap.put("flag", "1");
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //跳转修改权限信息页面
    @RequestMapping(value = "/toPageUpdatePrivilege", method = RequestMethod.GET)
    public String  toPageUpdatePrivilege(ModelMap model,String privilegeID) {
    	
    	model.put("privilegeID", privilegeID);
    	
        return "page/role/updatePrivilege";
    }
    
    //修改权限
    @RequestMapping(value = "/updatePrivilege", method = RequestMethod.GET)
    @ResponseBody
    public String  updatePrivilege(String privilegeID,String showName,String code,String type,String level,String privilegeOrder,String moName,String pId,String remark ,String iconfile,String moIcon,String link) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	Privilege privilege = new Privilege();
    	if(privilegeID != null && !privilegeID.isEmpty()){
    		privilege.setId(Long.parseLong(privilegeID));
    	}
    	if(showName != null && !showName.isEmpty()){
    		privilege.setShowName(showName);
    	}
    	if(code != null && !code.isEmpty()){
    		privilege.setCode(code);
    	}
    	if(type != null && !type.isEmpty()){
    		privilege.setType(Integer.parseInt(type));
    	}
    	if(level != null && !level.isEmpty()){
    		privilege.setLevel(Integer.parseInt(level));
    	}
    	if(privilegeOrder != null && !privilegeOrder.isEmpty()){
    		privilege.setOrder(Integer.parseInt(privilegeOrder));
    	}
    	if(moName != null && !moName.isEmpty()){
    		privilege.setMobileMenuName(moName);
    	}
    	if(pId != null && !pId.isEmpty()){
    		Privilege parent = new Privilege();
    		parent.setId(Long.parseLong(pId));
    		privilege.setParent(parent);
    	}
    	if(remark != null && !remark.isEmpty()){
    		privilege.setRemark(remark);
    	}
    	if(iconfile != null && !iconfile.isEmpty()){
    		privilege.setIcon(iconfile);
    	}
    	if(moIcon != null && !moIcon.isEmpty()){
    		privilege.setMobileIcon(moIcon);
    	}
    	if(link != null && !link.isEmpty()){
    		privilege.setLink(link);
    	}
    	
    	roleService.updatePrivilege(privilege);
    	msgMap.put("flag", "1");
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //获取权限详情
    @RequestMapping(value = "/getPrivilege", method = RequestMethod.GET)
    @ResponseBody
    public String  getPrivilege(String privilegeID) {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	Privilege privilege = roleService.getPrivilege(Long.parseLong(privilegeID));
    	Privilege parent = privilege.getParent();
    	long pId = 0;
    	if(parent != null){
    		pId= parent.getId();
    	}
    	msgMap.put("privilege", privilege);
    	msgMap.put("pId", pId);
    	msgMap.put("flag", "1");
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
    
    //获取菜单导航
    @RequestMapping(value = "/getMenuList", method = RequestMethod.GET)
    @ResponseBody
    public String  getMenuList() {
    	String json = "";
    	Map<String ,Object> msgMap = new HashMap<String ,Object>();
    	
    	HttpSession session = request.getSession(); 
    	UserPrivate user = (UserPrivate) session.getAttribute("user");
    	
    	long roleID = user.getRole().getId();
    	List<Privilege> menuList = roleService.getMenuList(roleID);
    	msgMap.put("menuList", menuList);
    	msgMap.put("flag", "1");
    	
    	json = CommonUtil.mapToJsonString(msgMap);
        return json;
    }    
}
